<!--
  This file is a part of the open-eBackup project.
  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
  If a copy of the MPL was not distributed with this file, You can obtain one at
  http://mozilla.org/MPL/2.0/.
  
  Copyright (c) [2024] Huawei Technologies Co.,Ltd.
  
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  -->

<div class="aui-fullpage">
    <div class="aui-block vm-container">
        <lv-group class="group">
            <lv-group class="left" lvDirection='vertical'>
                <div class="aui-operation">
                    <lv-group lvGutter="16px">
                        <button lv-button lvType="primary" (click)="register()" auiRolePermission
                            [rolePermission]="roleOperationMap.manageResource">
                            {{'common_register_label' | i18n}}
                        </button>
                        <button lv-button lv-dropdown [lvDropdownMenus]="moreMenus"
                            [disabled]="!treeSelection[0] || (treeSelection[0] && treeSelection[0].parent)"
                            *ngIf="moreMenus | find: 'hidden': false">
                            <span>{{'common_more_label' | i18n}}</span>
                            <i #lvDropdownTrigger lv-icon="lv-icon-triangle-down"></i>
                        </button>
                        <button lv-button lvSize="auto" (click)="getTreeData()" class="aui-button-icon">
                            <i lv-icon="lv-icon-refresh" [lvColorState]="true"></i>
                        </button>
                    </lv-group>
                </div>
                <div class="tree-container">
                    <ng-container *ngIf="treeData.length;else noDataTpl">
                        <lv-tree lvCompareWith="uuid" lvSelectionAssociate="false" [lvData]='treeData'
                            [(lvSelection)]="treeSelection" lvSelectionMode='single' [lvBeforeSelected]="beforeSelected"
                            (lvCheck)="nodeCheck($event)" lvShowContentIcon (lvExpandedChange)="expandedChange($event)"
                            [lvNodeTemplate]='nodeTpl' [lvScroll]='virtualScroll.scrollParamMap["hcs-tree"]'
                            lvVirtualScroll>
                        </lv-tree>
                    </ng-container>
                </div>
            </lv-group>
            <div class="slip"></div>
            <lv-group class="right aui-paginator-container">
                <lv-tabs (lvActiveIndexChange)="tabIndexChange($event)" [(lvActiveIndex)]="activeIndex"
                    [lvAfterChange]="afterTabChange"
                    [ngClass]="{'right-tab':true,'aui-tab':true,'tab-single':treeSelection.length && (treeSelection[0].type===ResourceType.HOST)}">
                    <ng-container *ngFor="let tab of tabs">
                        <lv-tab *ngIf="!tab.hidden" lvTitle='{{tab.label}}({{tab.resourceTotal || 0}})' [lvId]="tab.id">
                            <ng-template lv-tab-lazy>
                                <aui-huawei-stack-list [tab]="tab" [path]="treeSelection[0] && treeSelection[0].path"
                                    [treeSelection]="treeSelection[0]" (updateTable)="updateTable($event)"
                                    [tableData]="tab.tableData" [isAuthCase]="treeData.length" #stkList>
                                </aui-huawei-stack-list>
                            </ng-template>
                        </lv-tab>
                    </ng-container>
                    <lv-tab *ngIf="treeSelection.length && treeSelection[0]?.type === ResourceType.PROJECT"
                        lvTitle="{{'protection_cloud_groups_label' | i18n}} ({{cloudGroupTotal}})"
                        [lvId]="ResourceType.CLOUD_GROUP">
                        <aui-virtualization-group [treeSelection]="treeSelection[0]"
                            [subUnitType]="dataMap.Resource_Type.HCSCloudHost.value"
                            (updateTable)="updateGroupTotal($event)"></aui-virtualization-group>
                    </lv-tab>
                </lv-tabs>
            </lv-group>
        </lv-group>
    </div>
</div>

<ng-template #noDataTpl>
    <div class="tree-no-data">
        <lv-empty lvDescription="{{'protection_vm_tree_no_data_label' | i18n}}"></lv-empty>
    </div>
</ng-template>

<ng-template #nodeTpl let-item>
    <div style="max-width: 200px;" lv-overflow>
        <span class="aui-gutter-row-xs">
            <i [lv-icon]="item.contentToggleIcon"></i>
        </span>
        <span [ngClass]="{'tree-node-offline': item.rootNodeLinkStatus === 0}">{{item.label}}</span>
    </div>
</ng-template>